package com.company.emenu.base;

import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.SessionAware;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.zfsoft.util.Tool;
import com.zfsoft.util.base.ColumnBean;

/**
 * @ClassName: BaseAction
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @author Rain
 * @date 2012-7-15 下午12:40:59
 */
public class BaseAction extends ActionSupport implements SessionAware {

	private Map<String, Object> session;

	public void setSession(Map<String, Object> session) {
		this.session = session;
	}

	public Map<String, Object> getSession() {
		return session;
	}
	
	public HttpServletRequest getRequest() {
		return (HttpServletRequest) ActionContext.getContext().get(
				ServletActionContext.HTTP_REQUEST);
	}

	public HttpServletResponse getResponse() {
		return (HttpServletResponse) ActionContext.getContext()
				.get(ServletActionContext.HTTP_RESPONSE);
	}

	@SuppressWarnings("unchecked")
	public String list(QueryParameter parameter, String path) {
		try {
			List<ExtTableBean> list = new ArrayList();
			ExtTableBean tableBean = new ExtTableBean();
			ColumnBean columnBean;
			FileInputStream fi = null;
			fi = new FileInputStream(path);
			SAXBuilder sb = new SAXBuilder();
			Document doc = sb.build(fi);
			Element el = doc.getRootElement();
			tableBean.setPrimaryName(el.getChildText("primaryName"));
			tableBean.setPageSize(el.getChildText("pageSize"));
			tableBean.setDataProxy(el.getChildText("dataProxy"));
			tableBean.setAjaxDelFun(el.getChildText("ajaxDelFun"));
			tableBean.setWindowWidth(el.getChildText("windowWidth"));
			tableBean.setWindowHeight(el.getChildText("windowHeight"));
			tableBean.setWindowSrcoll(el.getChildText("windowSrcoll"));
			tableBean.setTitNull(el.getChildText("titNull"));
			tableBean.setSearch(el.getChildText("search"));
			tableBean.setModiTitle(el.getChildText("modititle"));
			tableBean.setSelectState(el.getChildText("selectState"));
			tableBean.setDsState(el.getChildText("dsState"));
			Element column = el.getChild("column");
			List columns = column.getChildren();
			Iterator k = columns.iterator();
			List<ColumnBean> columnlist = new ArrayList<ColumnBean>();
			while (k.hasNext()) {
				Element fun = (Element) k.next();
				columnBean = new ColumnBean();
				columnBean.setColumnName(fun.getChildText("columnName"));
				columnBean.setColumnType(fun.getChildText("columnType"));
				columnBean.setHeader(fun.getChildText("header"));
				columnBean.setWidth(fun.getChildText("width"));
				columnBean.setSortable(fun.getChildText("sortable"));
				columnBean.setAlign(fun.getChildText("align"));
				columnBean.setIshidden(fun.getChildText("ishidden"));
				columnlist.add(columnBean);
			}
			tableBean.setColumn(columnlist.toArray());
			// parameter.setExtTable(tableBean);
			String colNames = "";
			String colModuel = "";
			int closize = columnlist.size();
			for (int o = 0; o < closize; o++) {
				ColumnBean col = columnlist.get(o);
				if (o == closize - 1) {
					colNames += "'" + col.getHeader() + "'";
					colModuel += "{name:'" + col.getColumnName() + "',index:'"
							+ col.getColumnName() + "',width:'"
							+ col.getWidth() + "',hidden:" + col.getIshidden()
							+ ",align:'" + col.getAlign() + "'}";
				} else {
					colNames += "'" + col.getHeader() + "',";
					colModuel += "{name:'" + col.getColumnName() + "',index:'"
							+ col.getColumnName() + "',width:'"
							+ col.getWidth() + "',hidden:" + col.getIshidden()
							+ ",align:'" + col.getAlign() + "'},";
				}
			}
//			String json = "{colNames:[" + colNames + "],colModuel:["
//					+ colModuel + "]}";
			String json = "["+colNames+"]&["+colModuel+"]";
			return json;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

}
